package com.lxg.controller.userController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxg.pojo.Books;
import com.lxg.pojo.Borrow;
import com.lxg.pojo.User;
import com.lxg.service.userService.UserBorrowService;
import com.lxg.service.userService.UserProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;


@Controller
public class UserJump {

    /*@RequestMapping("/userHome")
    public String readerHome(HttpSession session){
        User user = (User) session.getAttribute("user");
        String username = user.getUsername();
        session.setAttribute("username",username);
        return "/first/login_success";
    }*/

    @Autowired
    UserBorrowService userBorrowService;

    @RequestMapping("/music")
    public String tomusic(){
        return "user/music";
    }

    @GetMapping("/user/self")
    public String toSelf(){
        return "user/self";
    }

    @GetMapping("/user/myBook")
    public String toMyBook(Model model, HttpSession session, HttpServletRequest request, @RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize){

        String borrowResult = request.getParameter("borrowResult");
        if (borrowResult!=null ){
            if(borrowResult.equals("true") || borrowResult.equals("false")) {
                request.setAttribute("borrowResult",borrowResult);
            }
        }

        String maxResult = request.getParameter("maxResult");
        if(maxResult!=null){
            if ( maxResult.equals("true") || maxResult.equals("false")){
                request.setAttribute("maxResult",maxResult);
            }
        }


        String deleteResult = request.getParameter("deleteResult");
        if(deleteResult!=null){
            if ( deleteResult.equals("true") || deleteResult.equals("false")){
                request.setAttribute("deleteResult",deleteResult);
            }
        }


        User user = (User) session.getAttribute("user");
        if(user != null){
            Integer uid = user.getUid();
            PageHelper.startPage(pageNum,pageSize);
            ArrayList<Borrow> borrows = userBorrowService.showMyBooks(uid);
            if(borrows.size()==0){
                request.setAttribute("isEmpty","true");
                return "user/emptyMyBook";
            }else{
                PageInfo<Borrow> pageInfo = new PageInfo<>(borrows,3);
                model.addAttribute("pageInfo",pageInfo);
                return "user/myBook";
            }
        }else{
            request.setAttribute("noUser","true");
            return "user/emptyMyBook";
        }






    }

    @GetMapping("/user/problem")
    public String toProblem(){

        return "user/problem";
    }
}
